package com.jfreer.security;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * User: landy
 * Date: 15/6/1
 * Time: 下午10:40
 */
public class MyUserDetailService implements UserDetailsService {
    @Resource
    private SecurityData securityData;

    @Override
    public UserDetails loadUserByUsername(String name) throws UsernameNotFoundException {
        String pwd = securityData.findPassword(name);
        List<String> roles = securityData.getRoleListByUserName(name);
        List<GrantedAuthority> authrities = new ArrayList<GrantedAuthority>();
        for (String role : roles) {
            authrities.add(new SimpleGrantedAuthority(role));
        }
        return new User(name, pwd, authrities);
    }
}
